<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="收费站" prop="deptName" label-width="100px"  v-if="deptId==101">
        <el-tree-select v-model="queryParams.deptId"  :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
          placeholder="请选择收费站" check-strictly clearable />
      </el-form-item>
      <el-form-item label="月份" prop="month">
        <el-date-picker v-model="queryParams.month" type="month" placeholder="请选择" />
      </el-form-item>
      <el-form-item label="日期" prop="date">
        <el-date-picker v-model="queryParams.date" type="date" placeholder="请选择" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="radio1=='调车管理'">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['vehicle:shunting:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['vehicle:shunting:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shuntingList" @selection-change="handleSelectionChange">
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column label="月份" align="center" prop="companyName" />
      <el-table-column label="日期" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="值班站长" align="center" prop="vehicleNo" />
      <el-table-column label="收费" align="center">
        <el-table-column label="白班" align="center" prop="vehicleType" />
        <el-table-column label="夜班" align="center" prop="vehicleType" />
      </el-table-column>
      <el-table-column label="治超" align="center">
        <el-table-column label="白班" align="center" prop="vehicleType" />
        <el-table-column label="夜班" align="center" prop="vehicleType" />
      </el-table-column>
      <el-table-column label="票证稽核" align="center">
        <el-table-column label="主办" align="center" prop="vehicleType" />
        <el-table-column label="协办" align="center" prop="vehicleType" />
      </el-table-column>

      <el-table-column label="ETC发行" align="center" prop="deptName" />
      <el-table-column label="办公" align="center" prop="sourceDeptName" />
      <el-table-column label="维修" align="center" prop="sourceDeptName" />
      <el-table-column label="后倾" align="center" prop="sourceDeptName" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['vehicle:shunting:edit']">人员调整</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改调车明细对话框 -->
    <el-dialog title="人员调整" v-model="open" width="800px" append-to-body>
      <el-form ref="shuntingRef" :model="form" :rules="rules" label-width="120px" class="freeFormItem">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="调整人员" prop="unitName">
              <el-select class="respon" v-model="form.vehicleNo" placeholder="请选择调车类型" @change="bindChangeVehicleNo">
                <el-option v-for="dict in vehicleList" :key="dict.vehicleNo" :label="dict.vehicleNo" :value="dict.vehicleNo"></el-option>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="12"> <el-form-item label="岗位" prop="sourceDeptName">
              <el-select class="respon" v-model="form.vehicleNo" placeholder="请选择调车类型" @change="bindChangeVehicleNo">
                <el-option v-for="dict in vehicleList" :key="dict.vehicleNo" :label="dict.vehicleNo" :value="dict.vehicleNo"></el-option>
              </el-select>
            </el-form-item></el-col>

          <el-col :span="12">
            <el-form-item label="调整后人员" prop="vehicleNo">
              <el-select class="respon" v-model="form.vehicleNo" placeholder="请选择调车类型" @change="bindChangeVehicleNo">
                <el-option v-for="dict in vehicleList" :key="dict.vehicleNo" :label="dict.vehicleNo" :value="dict.vehicleNo"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="岗位" prop="shuntingType">
              <el-select class="respon" v-model="form.shuntingType" placeholder="请选择调车类型">
                <el-option v-for="dict in shunting_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="调整周期" prop="month">
              <el-date-picker v-model="form.month" type="month" placeholder="请选择" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="调整开始日期" prop="month">
              <el-date-picker v-model="form.month" type="date" placeholder="请选择" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调整结束日期" prop="month">
              <el-date-picker v-model="form.month" type="month" placeholder="请选择" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Shunting">
// import { listShunting, getShunting, delShunting, addShunting, updateShunting } from "@/api/charge/charge";
import { deptTreeSelect } from "@/api/system/user";
import useUserStore from '@/store/modules/user'


const { proxy } = getCurrentInstance();
const { shunting_type } = proxy.useDict('shunting_type');
const shuntingList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");
const radio1 = ref("调车管理")
const vehicleList = ref([])
const deptOptions = ref(undefined);
const userStore = useUserStore()
const userDeptName = ref("")
const deptId=ref()

const data = reactive({
  form: { shuntingType: 1 },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    companyName: null,
    deptName: null,
    unitName: null,
    vehicleNo: null,
  },
  rules: {
    
  }
});

const { queryParams, form, rules } = toRefs(data);



/** 查询调车明细列表 */
function getList() {
  // loading.value = true;
  // listShunting(queryParams.value).then(response => {
  //   shuntingList.value = response.rows;
  //   total.value = response.total;
  //   loading.value = false;
  // });
}

/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data;
    getUserist()
  });
};

const bindChangeVehicleNo = (val) => {


}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
  };
  proxy.resetForm("shuntingRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}


/** 新增按钮操作 */
function handleAdd() {
  form.value.unitName = userDeptName.value;
  form.value.shuntingType = "1"
  open.value = true;
  title.value = "添加调车明细";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  // getShunting(_id).then(response => {
  //   form.value = response.data;
  //   open.value = true;
  //   title.value = "修改调车明细";
  // });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["shuntingRef"].validate(valid => {
    if (valid) {
      // if (form.value.id != null) {
      //   updateShunting(form.value).then(response => {
      //     proxy.$modal.msgSuccess("修改成功");
      //     open.value = false;
      //     getList();
      //   });
      // } else {
      //   addShunting(form.value).then(response => {
      //     proxy.$modal.msgSuccess("新增成功");
      //     open.value = false;
      //     getList();
      //   });
      // }
    }
  });
}



/** 导出按钮操作 */
function handleExport() {
  proxy.download('vehicle/shunting/export', {
    ...queryParams.value
  }, `调车管理_${new Date().getTime()}.xlsx`)
}

const getUserist = () => {
  userStore.getInfo().then(res => {
    if (res.code == 200) {
      deptId.value = res.user.dept.deptId;
      form.value.unitName = res.user.dept.deptName
      userDeptName.value = res.user.dept.deptName
      queryParams.value.deptId=res.user.dept.deptId;
      getList();

    }

  })
}


getDeptTree();

</script>


<style scoped>
.margin-bottom {
  margin-bottom: 15px;
}

.respon {
  width: 100%;
}
.freeFormItem .el-form-item.el-form-item--default.asterisk-left {
  display: flex;
}
.el-form--default.el-form--label-top .el-form-item .el-form-item__labe {
  width: 100px;
  flex-shrink: 0;
}
</style>